<?php

namespace App\Dao\Product;

use App\Tool\Collect;
use Hyperf\DbConnection\Db;
use Yj\Daos\BaseDao;
use App\Model\Product\StoreProduct;
use Hyperf\Database\Model\Builder;
use Yj\Daos\BaseDaos;
use Yj\Daos\Verify;
use Hyperf\Utils\Collection;


/**
 * @var StoreProduct
 * @method $this params($params)
 * @method $this type($params)
 */
class StoreProductDao extends BaseDaos
{


    public function searchParams($params)
    {
        $search = $this->getSearch();
        $params->check('type', function ($type) use ($search) {
            $search->where('type', $type);
        });
       
        $params->check('is_show', function ($is_show) use ($search) {
            $search->where('is_show', $is_show);
        });
        $params->check('cid', [null, '0', 0], function ($cid) use ($search) {
            $builder = Db::table('store_product_cate')->where('cate_pid', $cid)
                ->select('product_id');
            $search->whereIn('id', $builder);
        });
        $params->check(['user_id', 'mubiao'], function ($user_id, $mubiao) use ($search) {
            $builder = Db::table('store_cart')->where('uid', $user_id)
                ->select('product_id as id');
            $search->whereIn('id', $builder);
        });

        return $search;
    }

    public function searchType(Collect $params)
    {
        $search = $this->getSearch();
        $store_stock = $params->get('store_stock', 2);
        $params->check('type', function ($type) use ($search, $store_stock) {
//            $search->where('type', $type);
            switch ((int)$type) {
                case 1:
                    $search->where(['is_show' => 1, 'is_del' => 0]);
                    break;
                case 2:
                    $search->where(['is_show' => 0, 'is_del' => 0]);
                    break;
                case 3:
                    $search->where(['is_del' => 0]);
                    break;
                case 4:
                    $search->whereHasIn('StoreProductAttrValue', function ($query) {
                        $query->where('stock', 0)->where('type', 0);
                    });
                    break;
                case 5:
                    $search->whereHasIn('StoreProductAttrValue', function ($query) use ($store_stock) {
                        $query->where('stock', '<', $store_stock)->where('type', 0);
                    });
                    break;
                case 6:
                    $search->where(['is_del' => 1]);
                    break;
            }
        });

        return $search;
    }

    public function lists(Collection $params)
    {
        $data = $this->params($params)->orderByDesc("id")->getList();
        return $data;
    }

    public function add(Collection $params)
    {
        $data = $this->create($params->toArray());
        return $data;
    }

    public function del(int $id)
    {

        $data = $this->DaoWhere(["id" => $id])->firstOrError();
        return $data->delete();

    }

    public function getCount(Collection $params)
    {
        return $this->type($params)->count();
    }



}
